﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Ext.Net;

namespace LongSpeed.WebSite.Main
{
    public partial class AdminDeskTop : Behaviors.Session
    {
        #region Data

        public static Behaviors.WebSiteConfig SiteConfig = null;

        private IList<Behaviors.Memu> _MemusData = null;
        private IList<Behaviors.User> _UsersData = null;
        private IList<Behaviors.Part> _PartsData = null;

        public IList<Behaviors.Memu> MemusData
        {
            get
            {
                if (_MemusData == null)
                {
                    LongSpeed.WebSite.Behaviors.Memu m = new LongSpeed.WebSite.Behaviors.Memu();
                    _MemusData = m.GetMemu();
                }

                return _MemusData;
            }
            set { _MemusData = value; }
        }
        public IList<Behaviors.User> UsersData
        {
            get
            {
                if (_UsersData == null)
                {
                    LongSpeed.WebSite.Behaviors.User u = new LongSpeed.WebSite.Behaviors.User();
                    _UsersData = u.GetUsers();
                }

                return _UsersData;
            }
            set { _UsersData = value; }
        }
        public IList<Behaviors.Part> PartsData
        {
            get
            {
                if (_PartsData == null)
                {
                    LongSpeed.WebSite.Behaviors.Part u = new LongSpeed.WebSite.Behaviors.Part();
                    _PartsData = u.GetPart();
                }

                return _PartsData;
            }
            set { _PartsData = value; }
        }
        #endregion

        #region Refresh
        protected void RefreshMemuData(object sender, StoreRefreshDataEventArgs e)
        {
            MemusData = null;
            this.BindMemuData();
        }
        protected void RefreshUserData(object sender, StoreRefreshDataEventArgs e)
        {
            UsersData = null;
            this.BindUserData();
        }
        protected void RefreshPartData(object sender, StoreRefreshDataEventArgs e)
        {
            PartsData = null;
            this.BindPartData();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !IsPostBack)
            {

            }
        }

        #region BindData
        private void BindMemuData()
        {
            this.MemuStore.DataSource = MemusData;
            this.MemuStore.DataBind();
        }
        private void BindUserData()
        {
            this.UserStore.DataSource = UsersData;
            this.UserStore.DataBind();
        }
        private void BindPartData()
        {
            this.StorePart.DataSource = PartsData;
            this.StorePart.DataBind();
        }
        #endregion

        [DirectMethod]
        public int SaveUser(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var user = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.User)) as Behaviors.User;

            if (user.Save()) return user.ID; else return 0;
        }
        [DirectMethod]
        public int SaveMemu(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var memu = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Memu)) as Behaviors.Memu;
            memu.ParentMemu = new Behaviors.Memu() { ID = 0 };
            if (memu.Save()) return memu.ID; else return 0;
        }

        [DirectMethod]
        public int SavePart(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var p = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Part)) as Behaviors.Part;

            if (p.Save()) return p.ID; else return 0;
        }
        [DirectMethod]
        public bool DeletePart(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var p = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Part)) as Behaviors.Part;
            if (p.ID == 1 || p.ID == 7)
            {
                return false;
            }
            else
            {
                return p.Delete();
            }
        }

        [DirectMethod]
        public bool DeleteUser(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var user = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.User)) as Behaviors.User;
            return user.Delete();
        }
        [DirectMethod]
        public bool DeleteMemu(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var m = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.Memu)) as Behaviors.Memu;
            return m.Delete();
        }

        protected void btnLoginOut_Click(object sender, DirectEventArgs e)
        {
            Behaviors.Session.LoginOut();
        }
    }
}